package step6;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.BadLocationException;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.util.LinkedList;
import java.lang.IndexOutOfBoundsException;
   class Exam0814 extends JFrame implements ActionListener
{
 JTextField tf;
 JTextPane jtp =new JTextPane();
 JScrollPane jsp;
 Container c;
 LinkedList ll=new LinkedList();
 public Exam0814(String title)
 {
 super(title);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setLocation(300,100);
 setSize(400,300);
 setDesign();
 setVisible(true);
 }
 public void setDesign()
 {
  c=getContentPane();
  jsp=new JScrollPane(jtp);
  tf=new JTextField();
  tf.requestFocus();
  c.add("Center",jsp);
  c.add("South",tf);
  tf.addActionListener(this);
 }
 public void actionPerformed(ActionEvent  e)
 {
  String str=tf.getText();
  StringTokenizer st=new StringTokenizer(str,",");
  try
  {
   while(st.hasMoreTokens())
   {
    ll.add(st.nextToken());
   }
   check();
  }
  catch(NoSuchElementException n)
  {
   JOptionPane.showMessageDialog(Exam0814.this,"잘못 입력하셨습니다.",
    "데이타 입력오류", JOptionPane.ERROR_MESSAGE );
   tf.setText("");
   tf.requestFocus();
   return;
  }
  finally
  {
   ll.clear();
   tf.setText("");
   tf.requestFocus();
  }
 }
 public void check()
 {
  String str1=ll.getFirst()+"";
  try
  {
   if(str1.equals("1")) show(1);
   else if(str1.equals("2"))
   {
    ll.add("");
    show(2);
   }
   else if(str1.equals("3"))
   {
    ll.add("");
    show(3);
   }
   else
   {
    JOptionPane.showMessageDialog(Exam0814.this,"잘못 입력하셨습니다.",
    "데이타 입력오류", JOptionPane.ERROR_MESSAGE );
    tf.setText("");
    tf.requestFocus();
    return;
   }
  }
  catch (IndexOutOfBoundsException i)
  {
   JOptionPane.showMessageDialog(Exam0814.this,"잘못 입력하셨습니다.",
   "데이타 입력오류", JOptionPane.ERROR_MESSAGE );
   tf.setText("");
   tf.requestFocus();
   return; 
  }
 }
 public void show(Integer f)
 {
  StyledDocument doc = jtp.getStyledDocument(); 
  Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 
  Style s = doc.addStyle("blue", def); 
  StyleConstants.setForeground(s, Color.blue); 
  s = doc.addStyle("red", def); 
  StyleConstants.setForeground(s, Color.red); 
  s = doc.addStyle("black", def); 
  StyleConstants.setForeground(s, Color.black); 
  s = doc.addStyle("green", def); 
  StyleConstants.setForeground(s, Color.green); 
  try
  {
   if(f==1)
      doc.insertString(doc.getLength(),"[채팅 상태] \t" + (ll.get(1)+"")+"\n", doc.getStyle("red"));    
   else if(f==2)
      doc.insertString(doc.getLength(),"[ 귓속말 ]    \t" + (ll.get(1)+"")+"님이  당신에게 :  "+(ll.get(2)+"")+"\n", doc.getStyle("blue"));   
   else  doc.insertString(doc.getLength(),"[ 쪽지 ]    \t" + (ll.get(1)+"")+"님이   "+(ll.get(2)+"")+"\n", doc.getStyle("green"));   
  }
  catch (BadLocationException ble)
  {
  } 
    } 
 public static void main(String[] args) 
 {
  new Exam0814("채팅창");
 }
}